package com.bing.entity;

import com.bing.Constant;
import com.bing.Utils;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * Created by kaibb on 15-10-22.
 */

@Entity
public class Question {

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    private long id;

    @Column(unique = true, nullable = false,name = "questionId")
    private int qid;  //问题ID
    @Transient
    private String content; //问题整个网页内容
    private int youYong; //有用数
    private int tongWen; //同问数
    private int huiDa; //回答数
    private int liuLan; //浏览数

    @Enumerated(value = EnumType.STRING)
    private Status status = Status.INIT;

    @Column(name = "accountId")
    private int aid;

    @Temporal(TemporalType.TIMESTAMP)
    private Date times;

    private String comeFrom;

    private int titleNum;

    private int contentNum;

    private int fengMi;


    public Date getTimes() {
        return times;
    }

    public void setTimes(Date times) {
        this.times = times;
    }

    public String getComeFrom() {
        return comeFrom;
    }

    public void setComeFrom(String comeFrom) {
        this.comeFrom = comeFrom;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public int getTitleNum() {
        return titleNum;
    }

    public void setTitleNum(int titleNum) {
        this.titleNum = titleNum;
    }

    public int getContentNum() {
        return contentNum;
    }

    public void setContentNum(int contentNum) {
        this.contentNum = contentNum;
    }

    public int getFengMi() {
        return fengMi;
    }

    public void setFengMi(int fengMi) {
        this.fengMi = fengMi;
    }


    public int getYouYong() {
        return youYong;
    }

    public void setYouYong(int youYong) {
        this.youYong = youYong;
    }

    public int getTongWen() {
        return tongWen;
    }

    public void setTongWen(int tongWen) {
        this.tongWen = tongWen;
    }

    public int getHuiDa() {
        return huiDa;
    }

    public void setHuiDa(int huiDa) {
        this.huiDa = huiDa;
    }

    public int getLiuLan() {
        return liuLan;
    }

    public void setLiuLan(int liuLan) {
        this.liuLan = liuLan;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public boolean toFile() {
        String pathname = Constant.BASE_PATH + File.separator + Constant.WEN_TI + File.separator + qid + ".html";
        File qfile = new File(pathname);
        try {
            qfile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Utils.str2File(pathname, this.content);
    }

    public int getQid() {
        return qid;
    }

    public void setQid(int qid) {
        this.qid = qid;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public int getAid() {
        return aid;
    }

    public void setAid(int aid) {
        this.aid = aid;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Question{");
        sb.append("id=").append(id);
        sb.append(", qid='").append(qid).append('\'');
        sb.append(", youYong=").append(youYong);
        sb.append(", tongWen=").append(tongWen);
        sb.append(", huiDa=").append(huiDa);
        sb.append(", liuLan=").append(liuLan);
        sb.append(", status=").append(status);
        sb.append(", aid='").append(aid).append('\'');
        sb.append(", times=").append(times);
        sb.append(", from='").append(comeFrom).append('\'');
        sb.append(", titleNum=").append(titleNum);
        sb.append(", contentNum=").append(contentNum);
        sb.append(", fengMi=").append(fengMi);
        sb.append('}');
        return sb.toString();
    }

    public static class Builder {

        private Question question;

        public Builder() {
            question = new Question();
        }

        public static Builder newBuilder() {
            return new Builder();
        }

        public Builder setQid(int qid) {
            question.setQid(qid);
            return this;
        }

        public Builder setYouYong(int num) {
            question.setYouYong(num);
            return this;
        }

        public Builder setTongWen(int num) {
            question.setTongWen(num);
            return this;
        }

        public Builder setHuiDa(int num) {
            question.setHuiDa(num);
            return this;
        }

        public Builder setLiuLan(int num) {
            question.setLiuLan(num);
            return this;
        }

        public Builder setContent(String content) {
            question.setContent(content);
            return this;
        }

        public Builder setStatus(Status status) {
            question.setStatus(status);
            return this;
        }


        public Question build() {
            return question;
        }
    }
}
